// SlagCar/Register/Register.js
const req = require('../../utils/request');
const baseURL = req.baseURL;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fieldList: [],
    validFlag: false,
    validObj:{},
    formData:{},
    system_id: 1164,

    isEdit: false,
    readonly: false,

    rowData: {row_id: 0},

    // 231216 liang 车辆经营者
    ownerOptions: [],
    // 231216 end
  //  231218 yan 增加车队名称（承运单位）
    clientOptions: [],

    // 231218 liang 增加退审原因显示
    refundFieldList: [
      {label: '退审原因',field_id: 'disapprove_reason',type: 'textarea',},
    ],
    // 231218 end
  },

  toggleStatus(){
    this.setData({readonly: !this.data.readonly})
  },

  // 记录验证表单项
  validIpt(e) {
    let { validObj, } = this.data
    this.data.validObj = {
      ...validObj,
      ...e.detail,
    };
  },
  //验证数据
  validForm(validObj){
    return new Promise((resolve, reject) => {
      let valid = Object.values(validObj).includes(false) ? false : true;
      resolve(valid)
    })
  },
  //字段修改
  bindChange(e){
    let detail = e.detail,
      field = detail.field,
      id = field.field_id || field.fieldName,
      type = field.type,
      val = detail.val;

    if (type === 'num' || type === 'currency' || type === 'percent'){
      val = val ? val : null
    }

    //赋值
    this.data.formData[id] = val;
    // 231216 liang 填写车牌号码，查询 车辆是否已有经营者
    // 231218 yan 注释
    // if(id === 'vehicle_no') this.searchVehicleNoOwner(val)
    // 231216 end

    // 231218 yan 填写车牌号码，查询 车辆是否已有车队名称（承运单位）
    if(id === 'vehicle_no') this.searchVehicleNoClient(val)
    // 231218 end

    console.log(id,val);
  },

  onSubmit(){
    let {formData,rowData,system_id,miniprogram_OpenID} = this.data;
    console.log(formData);

    this.data.validObj = {}
    this.setData({
      validFlag: !this.data.validFlag,
    });
    let validObj = this.data.validObj;

    this.validForm(validObj).then(async valid => {
      if(valid) { // 表单验证通过
        console.log('表单验证通过', formData);
        formData.row_id = rowData.row_id;
        wx.showLoading({mask: true});

        let post = {...formData,row_id: rowData.row_id || 0,miniprogram_OpenID},
          url = `ZT/DriverRegistration/${system_id}` + (post.row_id ? `/${post.row_id}` : '')
        console.log(formData);
        let method = rowData.row_id ? wx.$put : wx.$post;
        method(5,url,post).then(res =>{
          if (res.errcode != 0) return;
          // wx.$toast('提交成功，请等待管理员审核开通'); // 提示放下面 getrowdata 会遮住

          this.getRowData();
          wx.$toast('提交成功，请等待管理员审核开通');

          this.onSubscribeSingle('eSnNbzNyPzUS3g_5AQs12wtQiIQqD9FXE05yUigq84s') // 231218 增加模板信息订阅
        })


      } else {  // 表单验证不通过
        console.log('表单验证不通过')
        wx.$toast('必填项不能为空！')
      }
    })
  },
  getRowData(){
    let {system_id,miniprogram_OpenID} = this.data;
    wx.showLoading({mask:true})
    wx.$get(5,`ZT/DriverRegistration/${system_id}?miniprogram_OpenID=${miniprogram_OpenID}`).then(res => {
      if (res.errcode != 0) return;
      wx.hideLoading();
      let data = res.data.row[0] || {};
      this.setData({rowData: data,readonly: data.row_id})
      // this.setData({rowData: {},readonly: false})
    })
  },

  getWXInfo(){
    return new Promise(resolve => {
      wx.login({
        success: (res) => {
          if(res.code) {
            wx.showLoading({ mask: true, })
            wx.$get(1, 'Authorize/get_miniprogram_code2login', { code: res.code, }).then(res => {
              wx.hideLoading()
              if(res.errcode !== 0) return
              let data = res.data.row[0] || {};

              if (!data.miniprogram_OpenID )return;
              this.setData({
                miniprogram_OpenID: data.miniprogram_OpenID
              });
              resolve()

              /*if(data.token) {  // 已绑定账号，返回登录信息返回字段内容，与/api/v1/Authorize/Token接口返回的字段内容一致，这个接口如有返回Token信息就已登录系统进入首页，否则就进入绑定账号界面。
                wx.$toast('该微信号已有账号，请直接登录，或登录解绑后再注册！')
                // 存储登录信息
                // wx.$util.setLoginStorage(data)
                // wx.$getBarTitle(data.system_id);
                // 返回上一页
                setTimeout(() => wx.navigateBack({delta: 1}),1000)
              } else {  // 未绑定，获取openid注册
                this.setData({
                  miniprogram_OpenID: data.miniprogram_OpenID
                });
                this.getRowData();
                resolve()
              }*/
            })
          } else {
            wx.$toast('授权失败！')
            wx.navigateBack({delta: 1})
          }
        }
      })
    })
  },

  //获取模块字段
  getCustomField(table_id){
    let url = 'QX/PRO_select_field',
      post = {"table": table_id,"fieldname":""};
    return new Promise(resolve => {
      wx.$post(4,url,post).then(res =>{
        if (res.errcode != 0) return;
        let data = res.data.row || [];
        resolve(data);
      })
    })
  },

  async prepare(){
    await this.getWXInfo();
    // 231216 liang 添加车辆经营者选项
    //231218 yan 注释获取车辆经营者选项，增加获取车队名称（承运单位）选项
    // let ownerOptionsJson = await this.getOwnerOptions()
    let clientOptionsJson = await this.getCarClientList()
    this.setData({
      // ownerOptions: JSON.parse(ownerOptionsJson),
      clientOptions: JSON.parse(clientOptionsJson),
    });
    // 231216 end
    //231218 end

    this.getRowData();
    let fieldList = [
      {"entity":"e_driver_registration","label":"姓名","fieldName":"name","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"姓名","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"name","is_sys":false,"is_null":false,"field_Name":"姓名","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"司机类型","fieldName":"driver_type","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picklist","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"司机类型","referenceFilterCondition":"","subjectData":"","col":2,"option":"[{\"label\":\"内部司机\",\"isDefault\":false},{\"label\":\"挂靠司机\",\"isDefault\":false},{\"label\":\"外请司机\",\"isDefault\":false}]","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driver_type","is_sys":false,"is_null":false,"field_Name":"司机类型","sql_type":"nvarchar(25)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"手机号码","fieldName":"phone","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"手机号码","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"phone","is_sys":false,"is_null":false,"field_Name":"手机号码","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"车牌号码","fieldName":"vehicle_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车牌号码","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"vehicle_no","is_sys":false,"is_null":false,"field_Name":"车牌号码","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      // 231216 liang 增加 Order_client_name|车辆经营者 文本 传中文名；若录入的车牌号码可以匹配出车辆经营者，就不允许用户再录入修改
      // {"entity":"e_driver_registration","label":"车辆经营者","fieldName":"Order_client_name","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆经营者","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"Order_client_name","is_sys":false,"is_null":false,"field_Name":"车辆经营者","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null,},
      // 231218 yan 注释
      // {"entity":"e_driver_registration","label":"车辆经营者","fieldName":"Order_client_name","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picklist","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆经营者","referenceFilterCondition":"","subjectData":"","col":2,"option":ownerOptionsJson,"displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"Order_client_name","is_sys":false,"is_null":false,"field_Name":"车辆经营者","sql_type":"nvarchar(25)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      // 231216 end

      //231218 yan 按张小姐要求隐藏【车辆经营者】，增加选择【车队名称】（实际是选业务往来单位表的是承运单位的单位）
      //231225 yan 注释
      // {"entity":"e_driver_registration","label":"车队名称","fieldName":"fuwudanweiquanchen","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picklist","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆经营者","referenceFilterCondition":"","subjectData":"","col":2,"option":clientOptionsJson,"displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"fuwudanweiquanchen","is_sys":false,"is_null":false,"field_Name":"车辆经营者","sql_type":"nvarchar(25)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      //231218 end

      //231225 yan 车队名称修改为录入
      {"entity":"e_driver_registration","label":"车队名称(车辆登记证名称)","fieldName":"fuwudanweiquanchen","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车队名称","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"fuwudanweiquanchen","is_sys":false,"is_null":false,"field_Name":"车队名称","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      //231225 end

      // 241021 yan 增加所属公司、所属账套

      // end

      {"entity":"e_driver_registration","label":"身份证号","fieldName":"idcard_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"身份证号","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"idcard_no","is_sys":false,"is_null":true,"field_Name":"身份证号","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"身份证正面","fieldName":"idcard_front","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"身份证正面","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"idcard_front","is_sys":false,"is_null":true,"field_Name":"身份证正面","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"身份证背面","fieldName":"idcard_back","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"身份证背面","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"idcard_back","is_sys":false,"is_null":true,"field_Name":"身份证背面","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"驾驶证号","fieldName":"driver_license_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"驾驶证号","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driver_license_no","is_sys":false,"is_null":true,"field_Name":"驾驶证号","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"驾驶证图片","fieldName":"driver_license_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"驾驶证图片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driver_license_pic","is_sys":false,"is_null":true,"field_Name":"驾驶证图片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"资格证号","fieldName":"certificate_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"资格证号","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"certificate_no","is_sys":false,"is_null":true,"field_Name":"资格证号","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"资格证图片","fieldName":"certificate_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"资格证图片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"certificate_pic","is_sys":false,"is_null":true,"field_Name":"资格证图片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"行驶证号","fieldName":"driving_permit_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"行驶证号","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driving_permit_no","is_sys":false,"is_null":true,"field_Name":"行驶证号","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"行驶证图片","fieldName":"driving_permit_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"行驶证图片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driving_permit_pic","is_sys":false,"is_null":true,"field_Name":"行驶证图片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"营运证号","fieldName":"operating_permit_no","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"text","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"营运证号","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"operating_permit_no","is_sys":false,"is_null":true,"field_Name":"营运证号","sql_type":"nvarchar(50)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"营运证图片","fieldName":"operating_permit_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"营运证图片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"operating_permit_pic","is_sys":false,"is_null":true,"field_Name":"营运证图片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"工商执照图片","fieldName":"business_license_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"工商执照图片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"business_license_pic","is_sys":false,"is_null":true,"field_Name":"工商执照图片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"司机头像","fieldName":"driver_profile_photo","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"司机头像","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"driver_profile_photo","is_sys":false,"is_null":true,"field_Name":"司机头像","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"车辆全身照片","fieldName":"vehicle_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆全身照片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"vehicle_pic","is_sys":false,"is_null":true,"field_Name":"车辆全身照片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"车辆和车牌照片","fieldName":"vehicle_number_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆和车牌照片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"vehicle_number_pic","is_sys":false,"is_null":true,"field_Name":"车辆和车牌照片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration",uploadMethod: uploadFileUnAuth,"label":"车辆和灯箱照片","fieldName":"vehicle_lightbox_pic","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"picture","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"车辆和灯箱照片","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"vehicle_lightbox_pic","is_sys":false,"is_null":true,"field_Name":"车辆和灯箱照片","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"开户银行","fieldName":"deposit_bank","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"textarea","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"开户银行","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"deposit_bank","is_sys":false,"is_null":true,"field_Name":"开户银行","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
      {"entity":"e_driver_registration","label":"开票资料","fieldName":"invoicing_information","enableRepeat":false,"allowCover":false,"allowCreate":false,"forbidSave":false,"coverUserAndUnit":"","createUserAndUnit":"","forbidSaveUserAndUnit":"","description":"","defaultValue":"","type":"textarea","precision":0,"computeExpress":"","mainEntity":"","searchFields":"","showFields":"开票资料","referenceFilterCondition":"","subjectData":"","col":2,"option":"","displayFormat":"","attachmentShowType":"","attachmentSizeLimit":0,"length":"","system_id":1164,"table_id":"e_driver_registration","field_id":"invoicing_information","is_sys":false,"is_null":true,"field_Name":"开票资料","sql_type":"nvarchar(250)","footer_stats_type":0,"source_datetime_field":"","mainEntity_datetime_field_1":"","mainEntity_datetime_Comparator_1":"","mainEntity_datetime_field_2":"","mainEntity_datetime_Comparator_2":"","source_data_field":"","mainEntity_data_field":"","mainEntity_name":null},
    ]
    this.setData({fieldList})

  },

  // 231216 liang 查询车辆经营者
  async searchVehicleNoOwner(val) {
    let { fieldList, formData, rowData, } = this.data,
        ownerId = 'Order_client_name',
        ownerItem = fieldList.filter(item => [item.fieldName, item.field_id].includes(ownerId))[0]
    this.getOwnerOptions(val).then(rowJson => {
      let row = JSON.parse(rowJson)
      ownerItem.option = row.length ? row : this.data.ownerOptions
      if(row.length === 1) {
        ownerItem.default = {
          [ownerId]: row[0].label,
        }
      } else {
        ownerItem.default = this.data.rowData.row_id ? null : {}
      }
      this.setData({ fieldList, })
    })
  },
  // 获取车辆经营者
  getOwnerOptions(vehicleNo) {
    let url = `ZT/DriverRegistration/Get_car_inf?system_id=${this.data.system_id}`
    if(vehicleNo) url += `&vehicle=${vehicleNo}`
    return new Promise(resolve => {
      wx.$get(5, url).then(res =>{
        if (res.errcode != 0) return;
        let { row, } = res.data
        row.forEach(item => {
          let { customer_name, } = item
          item.label = customer_name;
        })
        resolve(JSON.stringify(row))
      })
    })
  },
  // 231216 end

  // 231218 liang 调起订阅通知
  onSubscribeSingle(tid) {
    return new Promise((resolve, reject) => {
      wx.requestSubscribeMessage({   // 调起消息订阅界面
        tmplIds: [tid],
        success: res => {
          let isAccept = res[tid] === 'accept'
          if(isAccept) wx.$toast('订阅成功！')
          resolve({
            accept: isAccept,
            tid,
          })
        },
        fail: err => {
          console.log('订阅失败！', err)
          resolve()
        },
      })
    })
  },
  // 231218 end

  //231218 yan 获取车队名称（承运单位）列表
  async searchVehicleNoClient(val) {
    let { fieldList, formData, rowData, } = this.data,
      ownerId = 'fuwudanweiquanchen',
      ownerItem = fieldList.filter(item => [item.fieldName, item.field_id].includes(ownerId))[0]
    this.getCarClientList(val).then(rowJson => {
      let row = JSON.parse(rowJson);
      ownerItem.option = row.length ? row : this.data.clientOptions
      if(row.length === 1) {
        ownerItem.default = {
          [ownerId]: row[0].label,
        }
      } else {
        ownerItem.default = this.data.rowData.row_id ? null : {}
      }
      this.setData({ fieldList, });
    })
  },
  getCarClientList(vehicleNo){
    let url = `ZT/DriverRegistration/Get_car_inf_cydw?system_id=${this.data.system_id}`
    if(vehicleNo) url += `&vehicle=${vehicleNo}`
    return new Promise(resolve => {
      wx.$get(5, url).then(res =>{
        if (res.errcode != 0) return;
        let { row, } = res.data
        row.forEach(item => {
          let { customer_name, } = item
          item.label = customer_name;
        })
        resolve(JSON.stringify(row))
      })
    })


  },
  //231218 end

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {cid,zid} = options;

    this.prepare();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

//231215 yan 复制上传文件专用接口修改-未登录时的上传接口
function uploadFileUnAuth(filePath, name) {
  let pages = getCurrentPages(),
    perpage = pages[pages.length - 1] || {},
    {system_id,miniprogram_OpenID} = perpage.data ||{};
   //当前页面
  console.log(perpage);
  console.log(system_id);
  console.log(miniprogram_OpenID);
  return new Promise((resolve, reject) => {
    // 参考 https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html
    // https://www.jianshu.com/p/cbb99904ebd2
    let url = `${baseURL}/api/v5/ZT/upload_training_materials_attachments?miniprogram_OpenID=${miniprogram_OpenID}&system_id=${system_id}`
    if(name) url += `?file_name=${name}`
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: 'file',
      formData: {
      },
      success: async(res) => {
        let root = JSON.parse(res.data).root
        resolve(root)
      },
    })
  })
}
